package com.happyfamily.springboot.crowdsourcingplatform.service.wang.impl;

import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dao.wang.ParameterDao;
import com.happyfamily.springboot.crowdsourcingplatform.model.Parameter;
import com.happyfamily.springboot.crowdsourcingplatform.service.wang.ParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//参数表查询
@Service
public class ParameterServiceImpl implements ParameterService
{
	@Autowired
	private ParameterDao parameterDao;

	//参数配置表类型选择下拉框
	@Override
	public List<String> selectType()
	{
		return parameterDao.selectType();
	}

	//新增参数
	@Override
	public String parameterAdd(String ptype, String pname)
	{
		SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
		String ptime=sd.format(new Date());
		String res="error";
		int index=parameterDao.parameterAdd(ptype,pname,ptime);
		if (index>0)
		{
			res="success";
		}
		return res;
	}

	//参数删除
	@Override
	public String parameterDelete(String pid)
	{
		String res="error";
		int index=parameterDao.parameterDelete(pid);
		if (index>0)
		{
			res="success";
		}
		return res;
	}

	//参数值更改
	@Override
	public String parameterUpdata(String ptype, String pname,String pid)
	{
		String res="error";
		int index=parameterDao.selectPname(ptype,pname);
		if (index<=0)
		{
			SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
			String ptime=sd.format(new Date());
			index=parameterDao.parameterUpdata(ptype,pname,ptime,pid);
			res="not";
			if (index>0)
			{
				res="success";
			}
		}
		return res;
	}

	//参数配置表格展示
	@Override
	public Results<Parameter> searchParameter(Integer startP,Integer endP,String parameterType,String starttime,String endtime)
	{
		return Results.success(parameterDao.searchCount(parameterType,starttime,endtime).intValue(),parameterDao.searchParameter(startP,endP,parameterType,starttime,endtime));
	}
}
